package com.java83.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMq配置类，定义交换机，队列，绑定
 */
@Configuration
public class RabbitmqConfiguration {

    /**
     * 定义交换机
     */
    @Bean
    public TopicExchange exchangetopic() {
        return new TopicExchange("exchangetopic");
    }

//    /**
//     * 定义队列
//     */
//    @Bean
//    public Queue queueemail() {
//        return new Queue("queueemail");
//    }


    /**
     * 绑定队列到交换机
     * 同类型的bean有多个，参数注入
     * (@Qualifier("queueemail") Queue queue)
     * ( Queue queueemail)
     */
    @Bean
    public Binding bindingEmail(TopicExchange exchange, Queue queueemail) {
        return BindingBuilder.bind(queueemail).to(exchange).with("#.email.#");
    }

    /**
     * 为队列绑定死信队列
     * @return
     */
    @Bean
    public Queue queueemail(){
        Map<String,Object> headers = new HashMap<>();
        headers.put("x-message-ttl",180000);
        headers.put("x-dead-letter-exchange","jhjSX");
        headers.put("x-dead-letter-routing-key","rkSX");
        return new Queue("queueemail",true,false,false,headers);
    }
    @Bean
    public DirectExchange SXExchange(){
        return new DirectExchange("jhjSX");
    }
    //死信
    @Bean
    public Queue queueSX(){
        return new Queue("tdSX");
    }
    @Bean
    public Binding bindingSX(DirectExchange exchange,Queue queueSX){
        return BindingBuilder.bind(queueSX).to(exchange).with("rkSX");
    }
    //解析json
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}